package vcardreader.utilities;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class ExtensionFileFilter extends FileFilter {
    private String description;
    private String extension;
  
    /**
    * Call this constructor with the file extension and the file description to 
    * get a FileFilter for the given extension
    * @param extension
    * @param description 
    */
    public ExtensionFileFilter(String extension, String description) {
        this.description = description;
        this.extension = extension;
    }
    
    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public boolean accept(File file) {
        if (file.isDirectory()) {
            return true;
        } else {
            String path = file.getAbsolutePath().toLowerCase();
            if (path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.') {
                return true;
            }
        }
        return false;
    }
}